package com.charity.contoller;

import com.charity.mapper.EntryMapper;
import com.charity.model.CommonResponse;
import com.charity.pojo.Admin;
import com.charity.pojo.Donation;
import com.charity.pojo.Entry;
import com.charity.pojo.User;
import com.charity.service.*;
import com.charity.utils.RSAUtil;
import com.alibaba.fastjson.JSONArray;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;

@RestController
@RequestMapping("/admin")
public class AdminController {

    public static final String PubKey = com.charity.utils.IOUtil.readResourceAsString("conf/PubKey");
    public static final String PriKey = com.charity.utils.IOUtil.readResourceAsString("conf/PriKey");

    @Autowired
    private AdminService adminService;

    @Autowired
    private CharityService charityService;

    @Autowired
    private EntryMapper entryMapper;

    @Autowired
    private DonationService donationService;

    @Autowired
    private EntryService entryService;

    private CommonResponse commonResponse = new CommonResponse();

    private RSAUtil rsaUtil = new RSAUtil();

    @GetMapping("/adminById")
    public CommonResponse adminById(Long id) throws Exception {
        Admin admin = adminService.adminById(id);
        if (admin != null) {
            admin.setRealName(rsaUtil.decrypt(admin.getRealName(), PriKey));
            return commonResponse.ok(admin);
        }
        return commonResponse.cuw("300", "没有该用户");
    }

    @PostMapping("/updateAdmin")
    public CommonResponse updateAdmin(@RequestBody Admin admin) throws Exception {
        Long id = adminService.adminId(admin.getUserName());
        if (id != null) {
            Integer result = adminService.updateAdmin(admin.getId(), admin.getUserName(), admin.getPassword(), rsaUtil.encrypt(admin.getRealName(), PubKey),
                     admin.getPhone(), admin.getEmail());
            if (result > 0) {
                return commonResponse.ok(result);
            }
        }
        return commonResponse.cuw("300", "更新失败");
    }

    @PostMapping("/deleteAdmin")
    public CommonResponse deleteAdmin(@RequestBody Admin admin) {
        Integer result = adminService.deleteAdmin(admin.getId());
        if (result > 0) {
            return commonResponse.ok(result);
        }
        return commonResponse.cuw("300", "删除失败");
    }

    @RequestMapping("/users")
    public CommonResponse users() throws Exception {
        List<User> users1 = adminService.users();
        List<User> users2 = new ArrayList<>();
        for (User user:users1) {
            user.setRealName(rsaUtil.decrypt(user.getRealName(), PriKey));
            user.setIdCard(rsaUtil.decrypt(user.getIdCard(), PriKey));
            users2.add(user);
        }
        String users = JSONArray.toJSONString(users2);
        return commonResponse.ok(users2);
    }

    @RequestMapping("/admins")
    public CommonResponse admins() throws Exception {
        List<Admin> admins1 = adminService.admins();
        List<Admin> admin2 = new ArrayList<>();
        for (Admin admin:admins1) {
            admin.setRealName(rsaUtil.decrypt(admin.getRealName(), PriKey));
            admin2.add(admin);
        }
        String users = JSONArray.toJSONString(admin2);
        return commonResponse.ok(admin2);
    }

    @RequestMapping("/donations")
    public CommonResponse donations() throws Exception {
        List<Donation> donations1 = adminService.donations();
        String donations = JSONArray.toJSONString(donations1);
        return commonResponse.ok(donations1);
    }

    @RequestMapping("/entrys")
    public CommonResponse entrys() throws Exception {
        List<Entry> entrys1 = adminService.entrys();
        String entrys = JSONArray.toJSONString(entrys1);
        return commonResponse.ok(entrys1);
    }

    @RequestMapping("/selByDSH")
    public CommonResponse selByDSH() {
        List<Donation> donations1 = donationService.selBySH();
        String donations = JSONArray.toJSONString(donations1);
        return commonResponse.ok(donations1);
    }

    @RequestMapping("/selByESH")
    public CommonResponse selByESH() {
        List<Entry> entrys1 = entryService.selBySH();
        String entries = JSONArray.toJSONString(entrys1);
        return commonResponse.ok(entrys1);
    }

    @PostMapping("updDSh")
    public CommonResponse updDSh(@RequestBody Donation donation) throws Exception {
        Integer integer = donationService.updSh(donation);
        if (integer > 0) {
            entryService.updEntryCol(donation.getEntryId(), donation.getDonationAmount());
            return commonResponse.ok(integer);
        }
        return commonResponse.cuw("300","更新失败");
    }

    @PostMapping("updESh")
    public CommonResponse updESh(@RequestBody Entry entry) throws Exception {
        Integer integer = entryService.updSh(entry);
        if (integer > 0) {
            return commonResponse.ok(integer);
        }
        return commonResponse.cuw("300","更新失败");
    }

    @RequestMapping("/selDWTG")
    public CommonResponse selDWTG() {
        List<Donation> donations1 = donationService.selWTG();
        String donations = JSONArray.toJSONString(donations1);
        return commonResponse.ok(donations1);
    }

    @PostMapping("/delD")
    public CommonResponse delD(@RequestBody Donation donation) throws Exception {
        Integer result = donationService.delD(donation.getId());
        if (result > 0) {
            return commonResponse.ok(result);
        }
        return commonResponse.cuw("300", "删除失败");

    }

    @RequestMapping("/selEWTG")
    public CommonResponse selEWTG() {
        List<Entry> entry = entryService.selWTG();
        String entries = JSONArray.toJSONString(entry);
        return commonResponse.ok(entry);
    }

    @PostMapping("/delE")
    public CommonResponse delE(@RequestBody Entry entry) throws Exception {
        Integer result = entryService.delD(entry.getId());
        if (result > 0) {
            return commonResponse.ok(result);
        }
        return commonResponse.cuw("300", "删除失败");

    }

    @RequestMapping("/selJQ")
    public CommonResponse selJQ() {
        List<Entry> entries1 = entryService.selJQ();
        String entries = JSONArray.toJSONString(entries1);
        return commonResponse.ok(entries1);
    }

    @GetMapping("/upWJ")
    public CommonResponse upWJ(@RequestBody Entry entry) throws Exception {
        Integer result = entryService.upWJ(entry);
        Integer d = donationService.updBC(entry.getId(), entry.getAccount());
        if (result > 0 && d != null) {
            return commonResponse.ok(result);
        }
        return commonResponse.cuw("300","更新失败");
    }

    @RequestMapping("/selFSQ")
    public CommonResponse selFSQ() {
        List<Entry> entries1 = entryService.selFSQ();
        String entries = JSONArray.toJSONString(entries1);
        return commonResponse.ok(entries1);
    }

    @PostMapping("/upFF")
    public CommonResponse upFF(@RequestBody Entry entry) throws Exception {
        Integer integer = entryService.upFF(entry);
        Integer d = donationService.updBC(entry.getId(), entry.getAccount());
        if (integer > 0 && d != null) {
            return commonResponse.ok(integer);
        }
        return commonResponse.cuw("300","更新失败");
    }

    @PostMapping("/upZJ")
    public CommonResponse upZJ(@RequestBody Entry entry) throws Exception {
        Integer integer = entryMapper.upZJ(entry.getId());
        if (integer > 0) {
            return commonResponse.ok(integer);
        }
        return commonResponse.cuw("300","更新失败");
    }

    @PostMapping("/upDFF")
    public CommonResponse upFF(@RequestBody Donation donation) throws Exception {
        String account = entryService.selAC(donation.getEntryId());
        donation.setAccount(account);
        Integer integer = donationService.upFF(donation.getId(), donation.getAccount());
        if (integer > 0) {
            entryService.updEntryCol(donation.getEntryId(), donation.getDonationAmount());
            return commonResponse.ok(integer);
        }
        return commonResponse.cuw("300","更新失败");
    }
}
